package com.whd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whd.entity.Dept;
import com.whd.entity.DeptExample;
import com.whd.entity.Emp;
import com.whd.entity.EmpExample;
import com.whd.mapper.DeptMapper;
import com.whd.mapper.EmpMapper;
import com.whd.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author whd
 * @date 2023/07/01 10:12:59
 */
@Service
@Transactional
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Override
    public PageInfo<Dept> fndAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dept> depts = deptMapper.selectByExample(null);
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        setDept(pageInfo.getList());
        return pageInfo;
    }

    @Override
    public int save(Dept dept) {
        return deptMapper.insert(dept);
    }

    @Override
    public int update(Dept dept) {
        return deptMapper.updateByPrimaryKeySelective(dept);
    }

    @Override
    public int delete(Integer id) {
        EmpExample empExample = new EmpExample();
        empExample.or().andDeptIdEqualTo(id);
        List<Emp> emps = empMapper.selectByExample(empExample);
        if (emps.size() == 0) {
            return -2;
        }
        List<Dept> depts = deptMapper.selectByExample(null);
        for (Dept dept : depts) {
            if (dept.getParentId() == id) {
                return  -1;
            }
        }
        return deptMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Dept> findByOther(Map<String, Object> map) {
        DeptExample deptExample = new DeptExample();
        if (map.get("parentId")!="undefined"){
            Integer parentId = (Integer) map.get("parentId");
            deptExample.or().andParentIdEqualTo(parentId);
        }
        String deptName = (String) map.get("deptName");
        deptExample.or().andNameLike("%"+deptName+"%");
        PageHelper.startPage(1, 5);
        List<Dept> list = deptMapper.selectByExample(deptExample);
        PageInfo<Dept> pageInfo = new PageInfo<>(list);
        setDept(pageInfo.getList());
        return pageInfo;
    }

    @Override
    public List<Dept> findAll() {
/*        List<Dept> list = findChildTree();
        List<Dept> deptList = new ArrayList<>();
        for (Dept dept : list) {
            deptList.add(dept);
            getFaltTree(deptList,dept);
        }*/
        return deptMapper.selectByExample(null);
    }

    private void setDept(List<Dept> list) {

        List<Dept> depts = deptMapper.selectByExample(null);

        for (Dept dept : list) {
            if(dept.getParentId()==null){
                continue;
            }
            for (Dept d : depts) {
                if (dept.getParentId()==d.getId()){
                    dept.setParent(d);
                }
            }

        }
    }

    private void setChildren(Dept parent, List<Dept> list) {
        for (Dept dept : list) {
            if (parent.getId() == dept.getParentId()) {
                parent.getChildren().add(dept);
                setChildren(dept, list);
            }
        }
    }
    private List<Dept> findChildTree() {
        List<Dept> list =  deptMapper.selectByExample(null);
        List<Dept> rootDepts = new ArrayList<Dept>();
        for (Dept dept : list) {
            if (dept.getParentId() == null) {
                rootDepts.add(dept);
                setChildren(dept, list);
            }
        }
        return rootDepts;
    }
    private void getFaltTree(List<Dept> depts, Dept parent) {
        if (parent.getChildren().size() > 0) {
            for (Dept dept : parent.getChildren()) {
                depts.add(dept);
                getFaltTree(depts,dept);
            }

        }
    }

}
